package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Article;
import com.example.domain.entity.Category;
import com.example.domain.vo.CategoryVo;
import com.example.mapper.CategoryMapper;
import com.example.service.ArticleService;
import com.example.service.CategoryService;
import com.example.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-05-09 16:20:21
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;


    /**
     * 获取文章分类列表
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {
        //查询所有正常状态的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,0);
        List<Article> list = articleService.list(queryWrapper);
        //文章分类id去重
        HashSet<Long> set = new HashSet<>();
        for (Article article :list) {
            set.add(article.getCategoryId());
        }
        //vo
        List<Category> categories = listByIds(set);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

}
